package com.biocar.controller;

import com.biocar.ResBean;
import com.biocar.bean.Approve;
import com.biocar.service.impl.ApproveServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.NoSuchElementException;

/**
 * @author quxiang
 * @date 2021/12/15 10:57
 */
@RequestMapping("/approve")
@Controller
@RestController
public class ApproveController {
    @Autowired
    ApproveServiceImpl approveService;

    /**
     *  查询所有审批人及审批顺序
     * @param id
     * @return
     * {
     *     "code": 20000,
     *     "message": "success",
     *     "data": [
     *         {
     *             "id": 1,
     *             "flowUserName": "李四",
     *             "flowSort": 1,
     *             "status": 1,
     *             "chargeId": 1
     *         },
     *         {
     *             "id": 2,
     *             "flowUserName": "张三",
     *             "flowSort": 2,
     *             "status": 1,
     *             "chargeId": 1
     *         },
     *         {
     *             "id": 5,
     *             "flowUserName": "张四",
     *             "flowSort": 3,
     *             "status": 1,
     *             "chargeId": 1
     *         },
     *         {
     *             "id": 3,
     *             "flowUserName": "王五",
     *             "flowSort": 4,
     *             "status": 1,
     *             "chargeId": 1
     *         }
     *     ]
     * }
     */
    @RequestMapping("/getApproveSort/{id}")
    public ResBean<List<Approve>> getApproveSort(@PathVariable int id)
    {
        try{
            List<Approve> list=approveService.getApproveSort(id);
            return ResBean.successWithObj(list);
        }catch (NoSuchElementException e)
        {
            return ResBean.failWithMsg("未找到指定内容");
        }
    }


    /**
     * 判断下一级审批是否可以执行
     * @param id 当前审批层级，例如当前层级为3，返回false，则第4级无法执行
     * @param chargeId
     * @return
     */
    @RequestMapping("/isApproveById")
    public ResBean<?> isApproveByid(@Param("id") int id,@Param("chargeId") int chargeId)
    {
        if(approveService.isApproveById(id,chargeId))
        {
            return ResBean.success();
        }
        else
        {
            return ResBean.failWithMsg("下一级无法审批");
        }
    }


}
